package ren.install.web.controller;

import com.alibaba.fastjson.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.util.HtmlUtils;
import ren.install.web.common.Constants;
import ren.install.web.config.ValueCenter;
import ren.install.web.websocket.Greeting;
import ren.install.web.websocket.HelloMessage;

import java.io.File;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

/**
 * Created with IntelliJ IDEA.
 *
 * @author: zhuyh
 * @date: 2018/11/21
 */
@Controller
public class IndexController {
    @Autowired
    private ValueCenter valueCenter;


    @GetMapping("/")
    public String index(Model model) throws Exception {
        model.addAttribute("title", Constants.HTML_TITLE);

        String projectPath = valueCenter.getDocsFilePath();

        List<String> projects = new ArrayList<>();
        File[] files = cn.hutool.core.io.FileUtil.ls(projectPath);

        for(File file:files){
            projects.add(file.getName());
        }
        Collections.sort(projects);



        model.addAttribute("projectJson", JSONObject.toJSONString(projects));
        model.addAttribute("projects", projects);


        return "index";
    }
}
